# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=repmgr
pkgver=5.3.1
pkgrel=0
pkgdesc="Replication Manager for PostgreSQL"
url="http://www.repmgr.org/"
arch="all"
license="GPL-3.0"
depends="cmd:ssh doas postgresql-client"
makedepends="postgresql-dev flex libxml2-dev libedit-dev"
pkgusers="postgres"
pkggroups="postgres"
subpackages="$pkgname-doc $pkgname-daemon"
source="$pkgname-$pkgver.tar.gz::https://github.com/2ndQuadrant/repmgr/archive/v$pkgver.tar.gz
	repmgr.conf.patch
	repmgrd.initd
	repmgr.doasd
	"

build() {
	./configure
	make USE_PGXS=1
}

check() {
	./repmgr --version
	./repmgrd --version
}

package() {
	depends="$depends postgresql$(pg_config --major-version)"

	make USE_PGXS=1 DESTDIR="$pkgdir" bindir=/usr/bin install

	install -D -m 640 -o postgres -g postgres \
		repmgr.conf.sample "$pkgdir"/etc/repmgr.conf

	cd "$pkgdir"

	mkdir -p usr/share/doc/$pkgname
	cp "$builddir"/*.md "$builddir"/doc/*.md usr/share/doc/$pkgname/

	install -D -m 600 "$srcdir"/repmgr.doasd etc/doas.d/$pkgname.conf
}

daemon() {
	pkgdesc="A management and monitoring daemon for PostgreSQL Replication Manager"
	depends="$pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/repmgrd "$subpkgdir"/usr/bin/

	install -D -m 755 "$srcdir"/repmgrd.initd "$subpkgdir"/etc/init.d/repmgrd
}

sha512sums="
1e6feae2668af9b28f68cac06e5d2d175b1df267397ed6a15587047311e55bc81391d08031b889105179a9e769c0703ae2c83c25501a178d7575cb4f6801d980  repmgr-5.3.1.tar.gz
a35975496b208424fb160e9d7d93b787c9b53070b6c795a892f437d5cadf25a67202052d9b4bcfa463ff970e0654fbe687bba57c6c08e369e1c3b8642557538a  repmgr.conf.patch
988d68205290aef2f1f63281d2dc7da7267ba2738d44a8908b0506e968d8acd895c03d9ee821d7ef5d240ec82b67e51fdfb2d9219e71237c1b0aa3f458ad134b  repmgrd.initd
bf2de989323712ee595607a5394bbd0fbadec88d2e46744f6d0b8fce142cf93630868ec7f7108866ca2beb6b29a6999f87fd04fe4d88fa33919f1cd7fc2c76fa  repmgr.doasd
"
